package com.personal.utils.excel.utils;

import com.personal.utils.excel.utils.bean.CellFormatBean;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Classname ImportRecordUtils
 * @Description TODO
 * @Date 2019/7/22 18:31
 * @Created by wenjun.dai
 */
@Slf4j
public class ImportRecordUtils {

    /**
     * 校验上传数据
     *
     * @param map
     * @param errorDataList
     * @param cellFormatBeanList
     * @return
     */
    public static Boolean validData(Map<String, String> map, List<Map<String, String>> errorDataList,
                                    List<CellFormatBean> cellFormatBeanList) {
        Set<String> keySet = map.keySet();
        //空数据行标志符
        boolean rowIsNotEmpty = false;
        for (String key : keySet) {
            String value = map.get(key);
            if (null != value && !value.isEmpty()) {
                rowIsNotEmpty = true;
                boolean check = validKey(key, value);
                if (!check) {
                    //数据有问题
                    CellFormatBean cellFormatBean = new CellFormatBean(key, value);
                    cellFormatBeanList.add(cellFormatBean);
                    errorDataList.add(map);
                    return false;
                }
            }
        }
        //必填参数校验
        String phone = map.get("phone");
        if (rowIsNotEmpty && (null == phone || phone.isEmpty())) {
            //数据有问题
            errorDataList.add(map);
            return false;
        }
        return rowIsNotEmpty;
    }


    /**
     * 用户导入字段校验
     *
     * @param key
     * @param value
     * @return
     */
    private static boolean validKey(String key, String value) {
        boolean check;
        switch (key) {
            case "phone":
                check = checkPhone(value);
                break;
            case "contactName":
            case "qq":
            case "company":
            case "product":
            case "industry":
            case "wechat":
            case "type":
            case "level":
            case "points":
            case "status":
                check = checkLength(value, 30);
                break;
            case "postcode":
                check = checkLength(value, 6);
                break;
            case "address":
            case "remark":
                check = checkLength(value, 50);
                break;
            case "age":
                check = checkAge(value, 100);
                break;
            case "sex":
                check = checkSex(value);
                break;
            case "mail":
                check = checkEmail(value);
                break;
            case "tel":
                check = checkTel(value);
                break;
            case "regTime":
            case "birthday":
            case "createTime":
                check = checkCreateTime(value);
                break;
            default:
                check = true;
        }
        if (!check) {
            log.info(" key : {}, value : {}", key, value);
        }
        return check;
    }


    private static boolean checkCreateTime(String value) {
        SimpleDateFormat format;
        if (value.contains(" ")) {
            format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else {
            format = new SimpleDateFormat("yyyy-MM-dd");
        }
        Date parseDate;
        try {
            parseDate = format.parse(value);
        } catch (ParseException e) {
            log.error("时间解析异常! value = " + value, e);
            return false;
        }
        Date today = new Date();
        return parseDate.before(today);
    }


    private static boolean checkTel(String value) {
        if (null == value || value.isEmpty()) {
            return true;
        }
        return isTelePhoneWithAreaCode(value);
    }

    /**
     * 邮箱校验
     *
     * @param value
     * @return
     */
    private static boolean checkEmail(String value) {
        if (null == value || value.isEmpty()) {
            return true;
        }
        Pattern p1 = Pattern.compile("[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?"); // 验证带区号的
        Matcher m = p1.matcher(value);
        return m.matches();
    }

    /**
     * 性别校验
     *
     * @param value
     * @return
     */
    private static boolean checkSex(String value) {
        if (null == value || value.isEmpty() || value.equals("男") || value.equals("女") || value.equals("未知")) {
            return true;
        }
        return false;
    }

    /**
     * 年龄校验
     *
     * @param value
     * @param i
     * @return
     */
    private static boolean checkAge(String value, int i) {
        if (null == value || value.isEmpty() || Integer.parseInt(value) <= i) {
            return true;
        }
        return false;
    }

    /**
     * 判断字符是否超出限制
     *
     * @param targetStr
     * @param i
     * @return
     */
    private static boolean checkLength(String targetStr, int i) {
        if (null == targetStr || targetStr.isEmpty() || targetStr.length() <= i) {
            return true;
        }
        return false;
    }

    /**
     * 手机-固话校验
     *
     * @param phone
     * @return
     */
    private static boolean checkPhone(String phone) {
        if (null == phone || phone.isEmpty()) {
            return false;
        }
        //手机号校验
        boolean mobile = isMobile(phone);
        if (!mobile) {
            //固话校验
            mobile = isTelePhoneWithAreaCode(phone);
        }
        return mobile;
    }

    /**
     *  * 手机号验证
     *  * @param str
     *  * @return 验证通过返回true
     *  
     */
    public static boolean isMobile(final String str) {
        Pattern p = Pattern.compile("^(1[3-9])\\d{9}$"); // 验证手机号
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 电话号码验证
     *
     * @param str
     * @return 验证通过返回true
     */
    public static boolean isTelePhoneWithAreaCode(String str) {
        Pattern p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$"); // 验证带区号的
        Matcher m = p1.matcher(str);
        return m.matches();
    }

    /**
     * 电话号码验证
     *
     * @param str
     * @return 验证通过返回true
     */
    public static boolean isTelePhone(final String str) {
        Pattern p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$"); // 验证带区号的
        Pattern p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");     // 验证没有区号的
        if (str.length() > 9) {
            Matcher m = p1.matcher(str);
            return m.matches();
        } else {
            Matcher m = p2.matcher(str);
            return m.matches();
        }
    }
}
